home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / locale / SelfLoad / helloworld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-12  |  5.5 KB  |  159 lines

  1. /*
  2. helloworld.c - Special version showing catalog use with ot without locale
  3.  
  4. This version of HelloWorld.c shows how to use a couple of link modules
  5. (catalog.o and getcatalogstr.asm) to load your localized string catalog
  6. on systems running V37 (i.e. without V38 locale.library).
  7. The example will run under 1.3, but will use its internal strings
  8. only.  If you felt like modifying catalog.c by providing replacements
  9. for all of the 2.0 functions it uses, and adding a scheme for finding
  10. the PROGDIR where the catalogs can be found under 1.3, you could even
  11. make the catalog loading work under 1.3 + iffparse.library.  But you'll
  12. have to do that yourself.
  13.  
  14. When using these modules, use XOpenCatalog and XCloseCatalog
  15. when locale.library is not available, and ALWAYS use XGetString
  16. to get a string.  XGetString() will uses XGetCatalogStr() in
  17. getcatalogstr.asm which will use the locale.library GetCatalogStr()
  18. if locale has been opened, else it will use its own string-getting code.
  19. If you prefer to interface directly with XGetCatalogStr(), you
  20. can do that also - it has a stack-based interface matching that
  21. of the locale.library GetCatalogStr().
  22.  
  23. Note - the private variables of the XCatalog structure built by the
  24. link modules is purposely different from the real private Catalog
  25. structure variables in locale-allocated Catalogs.
  26.  
  27.  
  28. The one catch when loading your own catalogs:
  29.  
  30. On a <V38 system, there are no Locale Prefs, and therefore no system
  31. information about what language the user prefers.
  32.  
  33. When locale.library is available, you can use locale's OpenCatalog()
  34. with no specified language preference tag since locale.library will
  35. search for your catalog in the user's preferred order of languages.
  36.  
  37. When locale.library is not available, then you must use XOpenCatalog()
  38. with an explicit OC_Language tag since that is the only way the module
  39. can know what language to load.  This means your application will
  40. need to know what language the user wants.  You could do this having
  41. an icon tooltype specifying the language, or a command line arg, or
  42. some special file you set up when the user initially uses or installs
  43. your application.  In this simple example, we have just defined a string
  44. UBYTE *language which is the language we will open on pre-V38 systems.
  45. (The example only comes with that particular sample language catalog)
  46.  
  47. Note - do not ever attempt to locate/parse Locale.prefs yourself.
  48. The prefs file name and contents are subject to change.
  49. Use locale.library when available as this code does.
  50.  
  51.  
  52. Since a CatComp object module contains code which directly calls
  53. locale.library, we will instead use the CATCOMP_ARRAY in the .h file
  54. generated by CatComp instead of the object file option.  Any other
  55. application source modules that access strings should insetad
  56. #define CATCOMP_NUMBERS when including <appname>_strings.h. The
  57. catalog.c module will externally reference the catcomp string array
  58. which is included here.
  59.  
  60.  
  61. Sample use of CatComp for built-in strings:
  62. CatComp helloworld.cd cfile helloworld_strings.h
  63. sc structureequivalence nostackcheck helloworld.c
  64. slink LIB:c.o helloworld.o catalog.o getcatalogstr.o TO HelloWorld LIB LIB:sc.lib LIB:amiga.lib SC SD
  65. Quit
  66.  
  67. To create catalog files (after creating properly named directories for
  68. translations and catalogs (with correct accented characters in directory
  69. names, etc):
  70.  
  71. catcomp helloworld.cd CTFILE=translations/deutsch/helloworld.ct
  72. (edit the .ct file to add the translated strings)
  73. catcomp helloworld.cd translation=translations/deutsch/helloworld.ct (continued)
  74.         catalog=catalogs/deutsch/helloworld.catalog
  75. */
  76.  
  77. #include <exec/types.h>
  78. #include <libraries/locale.h>
  79. #include <utility/tagitem.h>
  80. #include <stdio.h>
  81. #include <dos.h>
  82.  
  83. #include <clib/exec_protos.h>
  84. #include <clib/dos_protos.h>
  85. #include <clib/locale_protos.h>
  86.  
  87. extern struct Library *SysBase;
  88. extern struct Library *DOSBase;
  89.  
  90. #include <pragmas/exec_pragmas.h>
  91. #include <pragmas/dos_pragmas.h>
  92. #include <pragmas/locale_pragmas.h>
  93.  
  94. #include "catalog.h"
  95.  
  96. /* Note - if you have other applications source files that access
  97.  * strings, in those modules you would include this CatComp-generated
  98.  * header defining CATCOMP_NUMBERS instead since the actual strings
  99.  * (i.e. the ARRAY) are only needed in one module, and they will
  100.  * be externally referenced by catalog.c
  101.  */
  102. #define CATCOMP_ARRAY
  103. #include "helloworld_strings.h"
  104.  
  105.  
  106. /* referenced by catalog.c */
  107. struct Library *LocaleBase  = NULL;
  108. struct Library *UtilityBase = NULL;
  109. struct Catalog *catalog = NULL;
  110. struct CatCompArrayType *StringArray = &CatCompArray[0];
  111. ULONG  StringCount = sizeof(CatCompArray) / sizeof(CatCompArray[0]);
  112.  
  113. UBYTE  *builtin  = "english";
  114. UBYTE  *language = "deutsch";
  115.  
  116.  
  117. VOID main(int argc, char **argv)
  118.     {
  119.  
  120.     /* This is required by catalog.c, as supplied, to load a catalog */
  121.     UtilityBase = OpenLibrary("utility.library",36);
  122.  
  123.     /* If available, we'll use locale.library */
  124.     LocaleBase  = OpenLibrary("locale.library",38);
  125.  
  126.     if(LocaleBase)
  127.         {
  128.     /* Using user's preferred language under 2.1 and higher */
  129.         catalog    = OpenCatalog(NULL,    "helloworld.catalog",
  130.                     OC_BuiltInLanguage, builtin,
  131.                     TAG_DONE);
  132.     }
  133.     else
  134.     {
  135.         catalog    = XOpenCatalog(NULL,"helloworld.catalog",
  136.                     OC_Language, language,
  137.                     OC_BuiltInLanguage, builtin,
  138.                     TAG_DONE);
  139.  
  140.     }
  141.  
  142.  
  143.     printf("%s\n",XGetString(MSG_HELLO));
  144.     printf("%s\n",XGetString(MSG_BYE));
  145.  
  146.  
  147.     if(LocaleBase)
  148.     {
  149.     if(catalog)    CloseCatalog(catalog);
  150.     CloseLibrary(LocaleBase);
  151.     }
  152.     else
  153.     {
  154.     if(catalog)    XCloseCatalog(catalog);
  155.     }
  156.  
  157.     if(UtilityBase)    CloseLibrary(UtilityBase);
  158.     }
  159.